package com.journey.org.ui.mine

import android.annotation.SuppressLint
import android.app.Application
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import androidx.databinding.ObservableList
import androidx.lifecycle.viewModelScope
import com.journey.org.BR
import com.journey.org.R
import com.journey.org.app.HttpManager
import com.journey.org.entity.WeatherData
import com.journey.org.ui.home.scan.ScanFragment
import com.journey.org.ui.login.LoginActivity
import com.journey.org.ui.mine.account.AccountFragment
import com.journey.org.ui.mine.collection.CollectionFragment
import com.mvvmhabit.org.base.BaseViewModel
import com.mvvmhabit.org.binding.command.BindingAction
import com.mvvmhabit.org.binding.command.BindingCommand
import com.mvvmhabit.org.utils.SPUtils
import kotlinx.coroutines.*
import me.tatarka.bindingcollectionadapter2.ItemBinding
import kotlin.coroutines.CoroutineContext

/**
 * @Date: 2020/11/17 14:18
 * @Author:小妖
 * @Description:
 */
class MineViewModel(application: Application) : BaseViewModel(application) {

    // 天气数据
    val onWeatherField = ObservableField<WeatherData>()

    val onHeaderUrlField =
        ObservableField("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202002%2F21%2F20200221083620_lfwvw.thumb.400_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1615260657&t=2dc93645d4a8513e68e205768a69f8cf")

    override fun onCreate() {
        super.onCreate()
        loadWeatherInfo("贵阳")
        loadOtherFunction()
    }

    @SuppressLint("CheckResult")
    private fun loadWeatherInfo(city: String) {
        // 运行在IO线程内容的协程
        viewModelScope.launch {
            val entity = withContext(Dispatchers.IO) {
                HttpManager.getInstance().loadWeatherInfo(city)
            }
            if (entity != null) {
                onWeatherField.set(entity.data)
            } else {
                cancel()
            }
        }
    }

    val onUserNameField =
        ObservableField(SPUtils.getInstance().getString(SPUtils.Config.USER_NAME, "登录/注册"))

    // 点击登录
    val onClickLoginCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            startActivity(LoginActivity::class.java, null)
        }
    })

    // 待定的其他方法
    private fun loadOtherFunction() {
        items.clear()
        items.add(MineOtherFunctionViewModel(this))
        items.add(MineOtherFunctionViewModel(this))
        items.add(MineOtherFunctionViewModel(this))
        items.add(MineOtherFunctionViewModel(this))
    }

    val items = ObservableArrayList<MineOtherFunctionViewModel>()

    val itemBinding =
        ItemBinding.of<MineOtherFunctionViewModel>(BR.viewModel, R.layout.mine_other_function_item)

    // 点击进入我的收藏
    val onClickEnterCollectionCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            startContainerActivity(CollectionFragment::class.qualifiedName)
        }
    })

    // 点击进入我的账号
    val onClickEnterAccountCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            startContainerActivity(AccountFragment::class.qualifiedName)
        }
    })

    /*********************************** 实现扫一扫功能 ************************************/
    val onClickScanCommand = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            startContainerActivity(ScanFragment::class.qualifiedName)
        }
    })


}